package com.cskaoyan.market.controller.admin;


import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.ObjectMetadata;
import com.cskaoyan.market.annotation.RequiresPermissionsDesc;
import com.cskaoyan.market.db.domain.MarketStorage;
import com.cskaoyan.market.service.AdminStorageService;
import com.cskaoyan.market.service.impl.AdminStorageServiceImpl;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/admin/storage/")
public class AdminStorageController extends HttpServlet {

    @Autowired
    AdminStorageService storageService;


    /**
     * @author 刘国兴
     * @description 获取存储对象列表
     */
    @RequiresPermissions("admin:storage:list")
    @RequiresPermissionsDesc(menu = {"系统管理", "对象存储"},button = "查询")
    @GetMapping("list")
    public Object list(@RequestParam("page") String page, @RequestParam("limit") String limit, @RequestParam("sort") String sort, @RequestParam("order") String order, @RequestParam(value = "name",required = false,defaultValue = "") String name, @RequestParam(value = "key",required = false,defaultValue = "") String key){
        List<MarketStorage> storageList=storageService.list(page,limit,sort,order,name,key);
        return ResponseUtil.okList(storageList);
    }

    /**
     * @author 刘国兴
     * @description 更新存储对象信息
     */
    @RequiresPermissions("admin:storage:update")
    @RequiresPermissionsDesc(menu = {"系统管理", "对象存储"},button = "更新")
    @PostMapping("update")
    public Object update(@RequestBody MarketStorage marketStorage){
        MarketStorage storage=storageService.update(marketStorage);
        return ResponseUtil.ok(storage);
    }

    /**
     * @author 刘国兴
     * @description 删除存储对象
     */
    @RequiresPermissions("admin:storage:delete")
    @RequiresPermissionsDesc(menu = {"系统管理", "对象存储"},button = "删除")
    @PostMapping("delete")
    public Object delete(@RequestBody MarketStorage marketStorage){
        storageService.delete(marketStorage);
        return ResponseUtil.ok();
    }


    @RequiresPermissions("admin:storage:create")
    @RequiresPermissionsDesc(menu = {"系统管理", "对象存储"},button = "新增")
    @PostMapping("create")
    @Transactional
    public Object createStorage(MultipartFile file) throws IOException {
        // OSS
        String accessKeyId = "LTAI5t8gpxPTCR6W58RnZq4u";
        String accessKeySecret = "mVPbpbxia0JQotb7HyJAREV8QUuq8h";
        String endPoint = "oss-cn-beijing.aliyuncs.com";
        String bucket = "cskaoyan";
        ObjectMetadata objectMetadata = new ObjectMetadata();

        String type = file.getContentType();

        objectMetadata.setContentType(type);
        OSS ossClient = new OSSClientBuilder().build(endPoint,accessKeyId,accessKeySecret);

        String name = file.getName();
        String fileName= UUID.randomUUID().toString().replaceAll("-","")+".png";
        objectMetadata.setContentDisposition("inline;filename="+fileName);
        // 执行文件上传，通过如下路径来进行访问：
        //https://{bucketName}.{endPoint}/filename
        ossClient.putObject(bucket,fileName,file.getInputStream(),objectMetadata);

        int size= (int) file.getSize();
        MarketStorage marketStorage = new MarketStorage();
        marketStorage.setKey(fileName);
        marketStorage.setName(name);
        marketStorage.setType(type);
        marketStorage.setSize(size);
        marketStorage.setUrl("https://cskaoyan.oss-cn-beijing.aliyuncs.com/"+fileName);
        marketStorage.setAddTime(LocalDateTime.now());
        marketStorage.setUpdateTime(LocalDateTime.now());
        // 执行文件上传，通过如下路径来进行访问：
        //https://{bucketName}.{endPoint}/filename
        // 写入数据库
        storageService.insert(marketStorage);
        return ResponseUtil.ok(marketStorage);
    }
}
